package com.jianyun.jyzs.model;

import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.jianyun.jyzs.bean.LoginBean;
import com.jianyun.jyzs.http.Api;
import com.jianyun.jyzs.http.RetrofitHelper;
import com.jianyun.jyzs.model.imdoel.IFriendDetailModel;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yangshijun on 2018/9/21.
 */

public class FriendDetailModel implements IFriendDetailModel {

    private static FriendDetailModel model;
    private static Gson gson;

    private FriendDetailModel() {
    }

    public static FriendDetailModel getInstance() {
        if (model == null)
            model = new FriendDetailModel();
        gson = new Gson();
        return model;
    }

    @Override
    public void addContact(String enterpriseCode, String userId, String commonContactId, final OnAddContactLisener onAddContactLisener) {
        Api api = RetrofitHelper.builderErpHttp().create(Api.class);
        api.addContacts("AddCommonContact", enterpriseCode, userId, commonContactId
                , "jyzs", "Android", "1.0.2")
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Object>() {
                    @Override
                    public void onNext(Object o) {
                        try {
                            String s = gson.toJson(o);
                            LoginBean loginBean = gson.fromJson(s, LoginBean.class);
                            if (loginBean.isResult()) {
                                onAddContactLisener.onSuccess();
                            } else {
                                onAddContactLisener.onFailed();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            onAddContactLisener.onFailed();
                        }
                    }

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        onAddContactLisener.onFailed();
                    }
                });
    }

    @Override
    public void deleteContact(String enterpriseCode, String userId, String recordId, final OnDeleteContactListener onDeleteContactListener) {
        Api api = RetrofitHelper.builderErpHttp().create(Api.class);
        api.delContact("DeleteCommonContact", enterpriseCode, userId, recordId
                , "jyzs", "Android", "1.0.2")
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Object>() {
                    @Override
                    public void onNext(Object o) {
                        //Log.i("test", "删除：" + o.toString());
                        try {
                            String s = gson.toJson(o);
                            LoginBean loginBean = gson.fromJson(s, LoginBean.class);
                            if (loginBean.isResult()) {
                                onDeleteContactListener.onSuccess();
                            } else {
                                onDeleteContactListener.onFailed();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            onDeleteContactListener.onFailed();
                        }

                    }

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        onDeleteContactListener.onFailed();
                    }
                });
    }
}
